package com.aidex.biz.controller;

import java.util.List;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.TempShare;
import com.aidex.biz.service.TempShareService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 分配临时Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/biz/bizTempShare")
public class TempShareController extends BaseController {
    @Autowired
    private TempShareService tempShareService;

    /**
     * 查询分配临时列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(TempShare tempShare, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            tempShare.setCompanyId(null);
        } else {
            tempShare.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        tempShare.setPage(new PageDomain(request, response));
        return R.data(tempShareService.findPage(tempShare));
    }

    /**
     * 获取分配临时详细信息
     */
    @GetMapping(value = "/{id}")
    public R<TempShare> detail(@PathVariable("id") String id) {
        return R.data(tempShareService.get(id));
    }

    /**
     * 新增分配临时
     */
    @Log(title = "分配临时", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated TempShare tempShare) {
        return R.status(tempShareService.save(tempShare));
    }

    /**
     * 修改分配临时
     */
    @Log(title = "分配临时", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated TempShare tempShare) {
        return R.status(tempShareService.save(tempShare));
    }


    /**
     * 删除分配临时
     */
    @Log(title = "分配临时", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(tempShareService.deleteTempShareByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(tempShareService.findMaxSort(new TempShare()));
    }

    /**
     * 导出分配临时列表
     */
    @Log(title = "分配临时", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(TempShare tempShare) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            tempShare.setCompanyId(null);
        } else {
            tempShare.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<TempShare> list = tempShareService.findList(tempShare);
        ExcelUtil<TempShare> util = new ExcelUtil<TempShare>(TempShare.class);
        return util.exportExcel(list, "分配临时数据");
    }

}
